package com.z.blog.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.extension.toolkit.SqlHelper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.z.base.vo.PageVo;
import com.z.blog.entity.Blog;
import com.z.blog.mapper.BlogMapper;
import com.z.blog.mapper.CommentMapper;
import com.z.blog.mapper.TagMapper;
import com.z.blog.service.IBlogService;
import com.z.blog.service.ICommentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import java.util.stream.Collectors;

/**
 * 博客Service业务层处理
 *
 * @author z
 * @date 2022-08-31
 */
@Service
@Transactional
public class BlogServiceImpl extends ServiceImpl<BlogMapper,Blog> implements IBlogService {

    @Autowired
    private ICommentService commentService;
    @Autowired
    private TagMapper tagMapper;

    @Autowired
    private CommentMapper commentMapper;

    @Override
    public PageInfo<Blog> selectBlogPage(Blog blog, PageVo pageVo) {
        PageHelper.startPage(pageVo.getPage(), pageVo.getLimit());
        List<Blog> data = baseMapper.selectBlogList(blog);
        return new PageInfo<>(data);
    }
    @Override
    public PageInfo<Blog> selectBlogListByS(Blog blog, PageVo pageVo) {
        PageHelper.startPage(pageVo.getPage(), pageVo.getLimit());
        List<Blog> data = baseMapper.selectBlogListByS(blog);
        return new PageInfo<>(data);
    }

    @Override
    public PageInfo<Blog> selectBlogPageByTagId(Long tagId, PageVo pageVo) {
        PageHelper.startPage(pageVo.getPage(), pageVo.getLimit());
        Blog blog = new Blog();
        blog.setPublished(1);
        blog.setTag(tagId+"");
        List<Blog> data = baseMapper.selectBlogListByTagId(blog);
        return new PageInfo<>(data);
    }

    @Override
    public List<Blog> listRecommendBlogTop(int top) {
        PageHelper.startPage(1, top);
        return baseMapper.selectList( new QueryWrapper<Blog>().eq("published", 1).eq("recommend", 1).orderByDesc("update_time"));
    }

    @Override
    public List<Blog> listHotBlogTop(int top) {
        PageHelper.startPage(1, top);
        return baseMapper.selectList( new QueryWrapper<Blog>().eq("published", 1).orderByDesc("views,share_statement"));
    }

    @Override
    public boolean deleteById(int id) {
        Map<String, Object> map = new HashMap<>();
        map.put("blog_id", id);
        tagMapper.deleteCenterTagIdsByBlogId(Long.parseLong(id+""));
        commentService.removeByMap(map);
        if(baseMapper.deleteById(id)>0){
            return true;
        }
        return false;
    }
    @Override
    public boolean removeByIds(Collection<?> list) {
        tagMapper.deleteCenterTagIdsByBlogIds(list);
        commentMapper.deleteCommentByBlogIds(list);
        return CollectionUtils.isEmpty(list) ? false : SqlHelper.retBool(this.getBaseMapper().deleteBatchIds(list));
    }

    @Override
    public boolean updateById(Blog blog) {
        try {
            List<Long> tags_ids = Arrays.asList(blog.getTag().split(",")).stream().map(o -> Long.parseLong(o)).collect(Collectors.toList());
            super.updateById(blog);
            tagMapper.deleteCenterTagIdsByBlogId(blog.getId());
            for(Long tagId : tags_ids){
                tagMapper.insertCenterTagId(tagId, blog.getId());
            }
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException("回滚");
        }

        return true;
    }

    @Override
    public void updateViews(Blog blog) {
        baseMapper.updateById(blog);
    }

    @Override
    public boolean save(Blog blog) {
        try {
            List<Long> tags_ids = Arrays.asList(blog.getTag().split(",")).stream().map(o -> Long.parseLong(o)).collect(Collectors.toList());
            super.save(blog);

            for(Long tagId : tags_ids){
                tagMapper.insertCenterTagId(tagId, blog.getId());
            }
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException("回滚");
        }
        return true;
    }
}
